//
// Created by work on 24-6-6.
//

#include "iot_model_device/CRtModelYkInfo.h"

namespace iotplat {
    std::map<CRtModelYkInfo::EModelYkCol, std::string> CRtModelYkInfo::rtModelYkColToStringMap = {
            {CRtModelYkInfo::EEn_write, "en_write"},
            {CRtModelYkInfo::EValues, "valuesmap"}
    };

    std::map<CRtModelYkInfo::EModelYkCol, CValue::EValueType> CRtModelYkInfo::rtYkDataTypeByField = {
            {CRtModelYkInfo::EEn_write, CValue::IOT_Bool},
            {CRtModelYkInfo::EValues, CValue::IOT_String}
    };

    CRtModelYkInfo::CRtModelYkInfo() {
    }

    Json::Value CRtModelYkInfo::toJson() {
        Json::Value json;
        json[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = model_name;
        json[baseSigFieldName(BASE_SIG_ENUM::EUser)] = user;
        json[baseSigFieldName(BASE_SIG_ENUM::EName)] = name;
        json[baseSigFieldName(BASE_SIG_ENUM::EDescr)] = descr;
        json[baseSigFieldName(BASE_SIG_ENUM::ESn)] = sn;
        json[baseSigFieldName(BASE_SIG_ENUM::EGrouping)] = grouping;
        json[baseSigFieldName(BASE_SIG_ENUM::EConfigs)] = configs;
        json[ykFieldName(YK_ENUM::EEn_write)] = en_write;
        json[ykFieldName(YK_ENUM::EValues)] = this->valuesToJson();
        return json;
    }

    void CRtModelYkInfo::fromJson(Json::Value json) {
        model_name = json[baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
        user = json[baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
        name = json[baseSigFieldName(BASE_SIG_ENUM::EName)].asString();
        descr = json[baseSigFieldName(BASE_SIG_ENUM::EDescr)].asString();
        sn = json[baseSigFieldName(BASE_SIG_ENUM::ESn)].asInt();
        grouping = json[baseSigFieldName(BASE_SIG_ENUM::EGrouping)].asString();
        configs = json[baseSigFieldName(BASE_SIG_ENUM::EConfigs)];
        en_write = json[ykFieldName(YK_ENUM::EEn_write)].asBool();
        valuesFromJson(json[ykFieldName(YK_ENUM::EValues)]);
    }

    Json::Value CRtModelYkInfo::valuesToJson() const {
        Json::Value arr(Json::ValueType::arrayValue);
        for(const auto& it : values) {
            arr.append(it.toJson());
        }
        return arr;
    }

    void CRtModelYkInfo::valuesFromJson(const Json::Value &json) {
        values.clear();
        if(json.isArray())
            return;

        for(const auto& obj : json) {
            SYkValue value;
            value.fromJson(obj);
            values.push_back(value);
        }
    }
} // iotplat